package com.ebt.m.commons.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;

/**
 * @author archer.qi
 *         Created on 2017/5/8.
 */
public class ActivityUtils {

    public static void launchActivity(Context context, Class<?> cls, Bundle data) {
        Intent intent = new Intent(context, cls);
        if (data != null) {
            intent.putExtras(data);
        }
        context.startActivity(intent);
    }

    public static void launchActivity(Context context, Class<?> cls) {
        launchActivity(context, cls, null);
    }

    public static void launchActivityForResult(Activity context, Class<?> cls, Bundle data, int requestCode) {
        Intent intent = new Intent(context, cls);
        if (data != null) {
            intent.putExtras(data);
        }
        context.startActivityForResult(intent, requestCode);
    }

    public static void launchActivityForResult(Activity context, Class<?> cls, int requestCode) {
        launchActivityForResult(context, cls, null, requestCode);
    }

    public static void launchActivityClearTop(Context context, Class<?> cls) {
        Intent intent = new Intent(context, cls);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        context.startActivity(intent);
    }

    /**
     * Activity是否结束
     *
     * @param activity
     * @return
     */
    public static boolean isActivityActive(Activity activity) {
        if (activity == null || activity.isFinishing()) {
            return false;
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            if (activity.isDestroyed()) {
                return false;
            }
        }
        return true;
    }

    public static int[] getScreenSize(Activity activity) {
        if (activity != null) {
            DisplayMetrics outMetrics = new DisplayMetrics();
            activity.getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
            return new int[]{outMetrics.widthPixels, outMetrics.heightPixels};
        }
        return null;
    }

    public static float getScreenDensity(Activity activity) {
        if (activity != null) {
            DisplayMetrics outMetrics = new DisplayMetrics();
            activity.getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
            return outMetrics.density;
        }
        return 2;
    }

    public static float getScreenDensity(Context context) {
        if (context != null) {
            DisplayMetrics outMetrics = new DisplayMetrics();
            ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(outMetrics);
            return outMetrics.density;
        }
        return 2;
    }

    /**
     * 关闭键盘
     */
    public static void closeInput(Activity activity) {
        InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (inputMethodManager != null && activity.getCurrentFocus() != null) {
            inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus()
                    .getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }
}


